package com.frame.platform.user;


import java.util.List;
import javax.servlet.http.HttpServletRequest;
import com.frame.common.SystemConfig;
import com.frame.common.ValidateNumber;
import com.frame.pageform.*;
import com.frame.pageform.component.JsonObject;
import com.frame.platform.dept.DeptDao;
import com.frame.platform.dept.Wfdept;
import com.frame.session.*;

public class UserLoginPage extends PageForm{

	static{
		setForward("/platform/ok.jsp");
	}
	
	private String actionType;
	private String loginName;
	private String loginPwd;
	private String state;
	private List<Wfdept> deptList;
	private String deptId;
	private String checkNum;
	
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		HttpServletRequest request=RequestContext.getContext().getRequest();
		String json="";
		if("selectDept".equals(getActionType())){

		}else{
			if(getLoginName()==null || "".equals(getLoginName()))
				json="{\"state\":\"noname\"}";
			else if(getLoginPwd()==null || "".equals(getLoginPwd()))
				json="{\"state\":\"nopwd\"}";
			else{
				try{
				    boolean bl=true;
				    if(SystemConfig.getInstance().isLoginCheckNumber())
				        bl=ValidateNumber.checkAll(getCheckNum()==null?"":getCheckNum(), request.getSession());
				    
				    if(bl){
    				SessionOaUser user=new UserLoginOperator().login(getLoginName(), getLoginPwd());
    				if(user!=null){
    					UserDao dao=new UserDao();
    					dao.writeSysLog(user.getUserId(), "登录", "登录系统", RequestUtil.getIpAddr(getRequest()));
    					deptList=dao.getUserDeptList(user.getUserId());
    					if(deptList.size()<=0)
    					json="{\"state\":\"nodept\"}";
    					else{
    					    DeptDao deptDao=new DeptDao();
    					    if(deptList.size()>1){
    					        String d="",b="";
        					    for(int i=0;i<deptList.size();i++){
        					       if(i!=0){d+=",";b+=",";}
        			               d+=deptList.get(i).getId();
        			               b+=deptDao.getBoardId(deptList.get(i).getId());
        			            }
        					    user.setDeptId(d);
                                user.setBoardId(b);
    					    }else{
    			                user.setDeptName(deptList.get(0).getDeptname());
    			                user.setDeptId(deptList.get(0).getId());
    			                user.setBoardId(deptDao.getBoardId(deptList.get(0).getId()));
    			                user.setBoardName(dao.getDeptName(user.getBoardId()));
    			            }
    					    user.setDepts(deptList);
    						request.getSession().setAttribute(UserSession.getAdminUserSessionName(), user);
    						json="{\"state\":\"ok\"}";
    					}
    				}
				    }else
				        json="{\"state\":\"验证码错误!\"}";
				}
				catch(LoginException e){
					json="{\"state\":\""+e.getMessage()+"\"}";
				}
				catch(Exception e){
					throw e;
				}
			}
		}
		return JsonObject.HTMLDATA+json.toString();
	}

	public String getActionType() {
		return actionType;
	}

	public void setActionType(String actionType) {
		this.actionType = actionType;
	}

	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public String getLoginPwd() {
		return loginPwd;
	}

	public void setLoginPwd(String loginPwd) {
		this.loginPwd = loginPwd;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getDeptId() {
		return deptId;
	}

	public void setDeptId(String deptId) {
		this.deptId = deptId;
	}

	public List getDeptList() {
		return deptList;
	}

	public void setDeptList(List deptList) {
		this.deptList = deptList;
	}

    public String getCheckNum() {
        return checkNum;
    }

    public void setCheckNum(String checkNum) {
        this.checkNum = checkNum;
    }
	
}
